package com.guet.niit.Login.controller.admin;

import com.guet.niit.Admin.dao.AgentDaolmpl;
import com.guet.niit.Admin.dao.ChargingDaolmpl;
import com.guet.niit.Admin.dao.ChargingPileDaolmpl;
import com.guet.niit.Admin.dao.UserDaolmpl;
import com.guet.niit.Admin.pojo.Agent;
import com.guet.niit.Admin.pojo.Charging;
import com.guet.niit.Admin.pojo.ChargingPile;
import com.guet.niit.Admin.pojo.User;
import com.guet.niit.Login.pojo.AgentData;
import com.guet.niit.Login.pojo.StationData;
import com.guet.niit.util.SnowFlakeUtil;
import com.guet.niit.util.adminSwitchoverMenu;
import com.guet.niit.util.exitLogin;
import com.sun.javafx.stage.StageHelper;
import javafx.beans.property.LongProperty;
import javafx.beans.property.SimpleLongProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Optional;
import java.util.ResourceBundle;

public class ModifyAgentDataFxController implements Initializable {
    public static ModifyAgentDataFxController modifyAgentDataFxController;

    @FXML
    private MenuItem stationManage;

    @FXML
    private MenuItem stationPileManage;

    @FXML
    private MenuItem userManage;

    @FXML
    private MenuItem mainMenu;

    @FXML
    private MenuItem exit;

    @FXML
    private TextField m_agentName;

    @FXML
    private TextField m_password;

    @FXML
    private Text modifyType;

    @FXML
    private Text oriAgentId;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        modifyAgentDataFxController = this;
    }

    /**
     * 通过菜单子项进入充电站管理
     *
     * @param actionEvent
     */
    public void enterStationManageByMenuItem(ActionEvent actionEvent) {
        //弹出确认对话框询问用户
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setHeaderText(null);
        alert.setContentText("您确定要放弃修改数据吗？");
        Optional<ButtonType> result = alert.showAndWait();
        //用户选择放弃修改数据
        if (result.get()==ButtonType.OK) {
            //进入充电站管理
            String text = stationManage.getText();
            adminSwitchoverMenu.backtrack(ModifyAgentDataFxController.class, text);
            //调用dao层查询充电站表
            ChargingDaolmpl chargingDao = new ChargingDaolmpl();
            List<Charging> stations = chargingDao.chargingInquire();
            //读取充电站列表信息
            ChargingStationManageFxController.chargingStationManageFxController.readStationsInfo(stations);
        }else {
            alert.close();
        }
    }

    /**
     * 通过菜单子项进入充电桩管理
     *
     * @param actionEvent
     */
    public void enterStationPileManageByMenuItem(ActionEvent actionEvent) {
        //弹出确认对话框询问用户
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setHeaderText(null);
        alert.setContentText("您确定要放弃修改数据吗？");
        Optional<ButtonType> result = alert.showAndWait();
        //用户选择放弃修改数据
        if (result.get()==ButtonType.OK) {
            //进入充电桩管理
            String text = stationPileManage.getText();
            adminSwitchoverMenu.backtrack(ModifyAgentDataFxController.class, text);
            //调用dao层查询充电桩表
            ChargingPileDaolmpl chargingPileDao = new ChargingPileDaolmpl();
            List<ChargingPile> chargingPiles = chargingPileDao.chargingPileInquire();

            //读取充电桩列表信息
            ChargingPileManageFxController.chargingPileManageFxController.readStationPileInfo(chargingPiles);
        }else {
            alert.close();
        }
    }

    /**
     * 通过菜单子项进入用户管理
     *
     * @param actionEvent
     */
    public void enterUserManageByMenuItem(ActionEvent actionEvent) {
        //弹出确认对话框询问用户
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setHeaderText(null);
        alert.setContentText("您确定要放弃修改数据吗？");
        Optional<ButtonType> result = alert.showAndWait();
        //用户选择放弃修改数据
        if (result.get()==ButtonType.OK) {
            //进入用户管理
            String text = userManage.getText();
            adminSwitchoverMenu.backtrack(ModifyAgentDataFxController.class, text);
            //调用dao层查询用户表
            UserDaolmpl userDao = new UserDaolmpl();
            List<User> users = userDao.userInquire();

            //读取用户列表信息
            UserManageFxController.userManageFxController.readUserInfo(users);
        }else {
            alert.close();
        }
    }

    /**
     * 通过菜单子项进入主菜单
     *
     * @param actionEvent
     */
    public void enterMainMenuByMenuItem(ActionEvent actionEvent) {
        //弹出确认对话框询问用户
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setHeaderText(null);
        alert.setContentText("您确定要放弃修改数据吗？");
        Optional<ButtonType> result = alert.showAndWait();
        //用户选择放弃修改数据
        if (result.get()==ButtonType.OK) {
            //进入主菜单
            String text = mainMenu.getText();
            adminSwitchoverMenu.backtrack(ModifyAgentDataFxController.class, text);
        }else {
            alert.close();
        }
    }

    /**
     * 点击菜单项的退出项退出系统
     *
     * @param actionEvent
     */
    public void clickExit(ActionEvent actionEvent) {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setHeaderText(null);
        alert.setContentText("您确定要退出吗？");
        Optional<ButtonType> result = alert.showAndWait();
        if (result.get() == ButtonType.OK) {
//            Platform.exit();
            exitLogin.backtrack(ModifyAgentDataFxController.class);
        } else {
            alert.close();
        }
    }

    /**
     * 修改数据
     * @param mouseEvent
     */
    public void modifyData(MouseEvent mouseEvent) {
        //获取用户输入的数据
        String newAgentName = m_agentName.getText();
        String newPassword = m_password.getText();

        AgentDaolmpl agentDao = new AgentDaolmpl();
        //获取用户的修改类型
        String modifyTypeText = modifyType.getText();
        if (modifyTypeText.equals("修改")){
            //获取原先保存的代理商id
            String stringOriAgentId = oriAgentId.getText();
            long oriAgentId = Long.parseLong(stringOriAgentId);
            //封装实体类
            Agent agent = new Agent(oriAgentId,newAgentName,newPassword);
            //调用service层修改数据
            boolean condition = agentDao.agentModify(agent);
            if (condition){
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setHeaderText(null);
                alert.setContentText("修改成功!");
                alert.showAndWait();
            }
        }else {
            //封装实体类
            SnowFlakeUtil snowFlakeUtil = new SnowFlakeUtil(2,3);
            long newAgentId = snowFlakeUtil.nextId();
            Agent agent = new Agent(newAgentId,newAgentName,newPassword);
            //调用service层插入数据
            boolean condition = agentDao.agentAdd(agent);
            if (condition){
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setHeaderText(null);
                alert.setContentText("新增成功!");
                alert.showAndWait();
            }
        }
        //返回代理商查询界面，并回显数据
        //引用fxml样式
        Parent load = null;
        try {
            load = FXMLLoader.load(getClass().getResource("/fxml/admin/agentManage.fxml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        ObservableList<Stage> stageList = StageHelper.getStages();
        stageList.get(0).setScene(new Scene(load));
        //调用dao层查询代理商表
        List<Agent> agents = agentDao.agentInquire();

        //读取代理商列表信息
        AgentManageFxController.agentManageFxController.readAgentInfo(agents);
    }

    /**
     * 原数据回显
     * @param selectedItem
     */
    public void showData(AgentData selectedItem){
        //正常回显
        m_agentName.setText(selectedItem.getAgentName());
        m_password.setText(selectedItem.getPassword());
        //设置用户选择的修改类型
        modifyType.setText("修改");
        //保存原来的代理商id
        oriAgentId.setText(selectedItem.getAgentId()+"");
    }

    /**
     * 重置输入
     * @param mouseEvent
     */
    public void resetData(MouseEvent mouseEvent) {
        //清空用户输入的数据
        m_agentName.setText("");
        m_password.setText("");
    }

    /**
     * 关于我们
     * @param actionEvent
     */
    public void aboutUs(ActionEvent actionEvent) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setHeaderText("关于我们");
        alert.setContentText("充电桩管理系统由Power-station项目组人员开发，本管理系统目前已实现四个模块的增删改查。");
        alert.showAndWait();
    }
}

